package com.tstudy.laoshibang.profile;

import java.util.List;

import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.base.BaseApplication;
import com.tstudy.laoshibang.mode.Message;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
 * 系统消息页adapter
* @CopyRight xinweitech.cn
* @author wuyahui
* @E-mail wuyahui0124505@163.com
* @version 1.0.0
* @Date 2015年4月9日 上午11:24:26
 */
public class MessageListAdapter extends BaseAdapter{
	LayoutInflater mInflater;
	private List<Message> mItems;
	public MessageListAdapter() {
		 mInflater = LayoutInflater.from(BaseApplication.mContext);
	}
	
	@Override
	public int getCount() {
		return mItems == null ? 0:mItems.size();
	}
	
	public void setData(List<Message> items) {
		setData(items,true);
	}
	public void setData(List<Message> items,boolean isRefresh) {
		mItems = items;
		if (isRefresh) {
			notifyDataSetChanged();
		}
	}
	
	public List<Message> getData() {
		return mItems;
	}
	

	@Override
	public Object getItem(int position) {
		return mItems == null ? null:mItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		if(convertView ==  null){
			viewHolder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.message_list_item, null);
			viewHolder.hasReadIcon = (ImageView)convertView.findViewById(R.id.message_list_item_has_read_icon);
			viewHolder.title = (TextView) convertView.findViewById(R.id.message_list_item_title);
			viewHolder.createTime = (TextView) convertView.findViewById(R.id.message_list_item_create_time);
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		if(viewHolder != null){
			setConvertView(viewHolder,position,convertView);
		}
		return convertView;
	}
	
	public void setConvertView(final ViewHolder viewHolder,final int position,View view){
		final Message message = (Message) getItem(position);
		viewHolder.title.setText(message.title);
		viewHolder.hasReadIcon.setVisibility(message.isRead == 0?View.VISIBLE:View.INVISIBLE);
		viewHolder.createTime.setText(message.createTime);
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mActionCallBack != null) {
					mActionCallBack.onItemClick(position,message);
				}
			}
		});
		
	}
	
	static class ViewHolder{
		TextView title;
		TextView createTime;
		ImageView hasReadIcon;
	}
	
	public interface ActionCallBack{
		public void onItemClick(int position,Message order);
		public void onAvatorClick(long teachId);
	}
	ActionCallBack mActionCallBack;
	public void setActionCallBack(ActionCallBack actionCallBack){
		mActionCallBack = actionCallBack;
	}
}
